$().ready(function() {
	tri_set_fileupload();
	addtri_set_fileupload();
	get_hash();
	$("#submitTrimesterguide").click(function(){
		saveTrimesterguide();
	});
	$("#add_trimesterguide_form").click(function(){
		addTrimesterguideForm();
	});
	$("#addsubmitTrimesterguide").click(function(){
		addTrimesterguide();
	});
});
function list_trimesterguide(){
	var control;
	var asort = 0;
	html_head = {};
	trimesterguide_icons = {};
	xfunc = "trimesterguide/list_trimesterguide";
	
	data_sent = {};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				$(result.intID).each(function(k,v){
					asort = result.intID[k];
					trimesterguide_icons_panel = '';
					trimesterguide_icons_panel +='<tr>';
				    trimesterguide_icons_panel +='<td><img class="img-rounded" src="'+base_url+'assets/uploaded/img/thumbnail/'+result.strImgLink[k]+'" alt=""/></td>';
					    trimesterguide_icons_panel +='<td>'+result.strTitle[k]+'</td>';
					    trimesterguide_icons_panel +='<td><div class="readmore">'+result.strNote[k]+'</div></td>';
					    trimesterguide_icons_panel +='<td class="controls"><div class="btn-group">';
					    trimesterguide_icons_panel +='<button class="btn btn-primary" onclick="javascript:editTrimesterguide(\''+result.intID[k]+'\')"><i class="icon-edit"></i>Edit</button>';
					    trimesterguide_icons_panel +='<button class="btn btn-danger" onclick="javascript:delTrimesterguide(\''+result.intID[k]+'\', \''+result.strTitle[k]+'\')"><i class="icon-trash"></i>Delete</button>';
					    trimesterguide_icons_panel +='</td>';
					trimesterguide_icons_panel +='</tr>';
					trimesterguide_icons[result.intID[k]]=trimesterguide_icons_panel;
				});
				
				trimesterguide_html_panel = '<table class="table table-striped table-bordered" id="trimesterguide_table">';
				trimesterguide_html_panel += '<thead>';
					trimesterguide_html_panel += '<tr>';
						trimesterguide_html_panel += '<th class="thumb-photo">Thumbnail</th>';
						trimesterguide_html_panel += '<th class="note-title">Title</th>';
						trimesterguide_html_panel += '<th class="note-content">Note Content</th>';
						trimesterguide_html_panel += '<th>Control</th>';
					trimesterguide_html_panel += '</tr>';
				trimesterguide_html_panel += '</thead>';
				trimesterguide_html_panel += '<tbody>';
				for(x=0;x<=asort;x++){
					if($.trim(trimesterguide_icons[x])!=""){
						trimesterguide_html_panel +=trimesterguide_icons[x];
					}
								
				}		
				trimesterguide_html_panel += '</tbody>';		
				trimesterguide_html_panel += '</table>';
				
				$('#trimesterguide_content').html(trimesterguide_html_panel);
			    $('div.readmore').expander({
			        slicePoint: 120,
			        expandText: '<br /><span class="label label-info">Read More...</span>',
			        userCollapseText: '<br /><span class="label label-important">Hide Text</span>'
			    });
				$('#trimesterguide_table').dataTable({
					"sPaginationType": "full_numbers",
					"bLengthChange": false,
					"bSort": false
				});
			}
		});	
}
function addTrimesterguideForm(){
	$("#trimesterguide_content, #add_trimesterguide_form").hide();
	$("#addTrimesterguide").slideDown('slow', function(){
		$("#trimesterguide_content").hide();
		$("#addTrimesterguide").slideDown('slow', function(){
			$(".back-btn-addtrimesterguide a").attr("onclick","btn_back_addtrimesterguide()");
		});	
	});	
}
function addTrimesterguide(){
	var strNoteTitle = $("#addTrimesterguidestrNoteTitle").val();
	var strNotes = $("#addTrimesterguidestrNotes").val();
	var strImgLink = $("#addTrimesterguidestrImgLink").val();
	if(strNoteTitle==''){
		$("#errorTrimesterguide").html("<div class='alert alert-error'>Error: Put the title!</div>");
	} else if(strNotes=='') {
		$("#errorTrimesterguide").html("<div class='alert alert-error'>Error: Put the Notes!</div>");
	} else if(strImgLink=='') {
		$("#errorTrimesterguide").html("<div class='alert alert-error'>Error: Put the image thumbnail!</div>");
	} else {
		xfunc = "trimesterguide/save_trimesterguide";
		data_sent = {
				'strNoteTitle':strNoteTitle,
				'strNotes':strNotes,
				'strImgLink':strImgLink
		};
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				$("#errorTrimesterguide").hide();
				$("#addtrimesterguideSuccess").html('<div class="alert alert-success">Data was successfully saved!</div>');
				
				$("#addTrimesterguidestrNoteTitle").val('');
				$("#addTrimesterguidestrNotes").val('');
				$("#addTrimesterguidestrImgLink").val('');
				$(".alert-success").fadeOut(5000);
			}
		});
	}
}
function editTrimesterguide(intCode){
	xfunc = "trimesterguide/get_trimesterguide";
	data_sent = {
			'intCode':intCode
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			var trimesterguide_thumbnail = '<div class="imgThumbnail"><img class="img-rounded" width="86" src="'+base_url+'assets/uploaded/img/thumbnail/'+result.strImgLink+'" alt=""/></div>';
			$("#intCode").val(result.intCode);
			$("#strImgLink").val(result.strImgLink);
			$("#strTrimesterTitle").val(result.strTitle);
			$("#strTrimesterNote").val(result.strNote);
			$("#trimesterguide_thumbnail").html(trimesterguide_thumbnail);
			$("#trimesterguide_thumbnailLink").val(result.strImgLink);
		}
	});

	$("#add_trimesterguide_form").hide();
	$("#trimesterguide_content").hide();
	$("#editTrimesterguide").slideDown('slow', function(){
		$(".back-btn-trimesterguide a").attr("onclick","btn_back_trimesterguide()");
	});	
}
function saveTrimesterguide(){
	var intCode = $("#intCode").val();
	var strTitle = $("#strTrimesterTitle").val();
	var strNote = $("#strTrimesterNote").val();
	var strImgLink = $("#trimesterguide_thumbnailLink").val();
	xfunc = "trimesterguide/save_trimesterguide";
	data_sent = {
			'intCode':intCode,
			'strTitle':strTitle,
			'strNote':strNote,
			'strImgLink':strImgLink
	};
	
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			$("#trimesterguideSuccess").html('<div class="alert alert-success">Data was successfully updated!</div>');
			$(".alert-success").fadeOut(5000);
		}
	});
}
function delTrimesterguide(intCode, titleName){
	xfunc = "trimesterguide/delete_trimesterguide";
	data_sent = {
			'intCode':intCode
	};
	$('#label-delete').html(titleName);
    $('#deleteModal').modal('show');
    $('button#confirm').click(function(e){
        $('#deleteModal').modal('hide');
		
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				list_trimesterguide();
			}
		});
    });
}
function btn_back_addtrimesterguide(){
	$("#addTrimesterguide").hide();
	$("#trimesterguide_content").slideDown('slow', function(){
		$("#add_trimesterguide_form").show();
		list_trimesterguide();
	});	
}
function btn_back_trimesterguide(){
	$("#editTrimesterguide").hide();
	$("#trimesterguide_content").slideDown('slow', function(){
		$("#add_trimesterguide_form").show();
		list_trimesterguide();
	});	
}

function tri_set_fileupload(){
	$('#tri_input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	tri_file_upload();
}
function tri_file_upload(){
	$('.tri_fileupload').fileupload();
	$('.tri_fileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+v.name+'" />';
					$("#trimesterguide_thumbnailLink").val(v.name);			
					$(".imgThumbnail").hide();
					$(".imgThumbnail").slideDown('slow', function(){
						$(".imgThumbnail").html(html);
					});					
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}

//ADD DATA
function addtri_set_fileupload(){
	$('#addtri_input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	addtri_file_upload();
}
function addtri_file_upload(){
	$('.addtri_fileupload').fileupload();
	$('.addtri_fileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+v.name+'" />';
					$("#addTrimesterguidestrImgLink").val(v.name);			
					$("#addtrimesterguide_thumbnail").html(html);			
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}